define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'store/store/index' + location.search,
                    add_url: 'store/store/add',
                    edit_url: 'store/store/edit',
                    del_url: 'store/store/del',
                    multi_url: 'store/store/multi',
                    import_url: 'store/store/import',
                    table: 'store',
                }
            });

            var table = $("#table");


            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},

                        {field: 'class_id', title: __('Class_id'),events: Controller.api.events.class_id,formatter: Controller.api.formatter.class_id},

                        {field: 'users.nickname', title: __('Users_id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'open_times', title: __('Open_times'), operate: 'LIKE'},
                        {field: 'longitude', title: __('Longitude'), operate:'BETWEEN'},
                        {field: 'latitude', title: __('Latitude'), operate:'BETWEEN'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'type', title: __('Type'), operate: 'LIKE'},
                        {field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'logo_image', title: __('Logo_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'store_rule', title: __('Store_rule'), operate: 'LIKE'},
                        // {field: 'content', title: __('Content'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        // {field: 'hot', title: __('Hot')},
                        {field: 'browse', title: __('Browse')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            // 启动和暂停按钮
            $(document).on("click", ".btn-start", function () {
                //在table外不可以使用添加.btn-change的方法
                //只能自己调用Table.api.multi实现
                //如果操作全部则ids可以置为空
                var ids = Table.api.selectedids(table);
                Table.api.multi("changestatus", ids.join(","), table, this);
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                class_id: function (value, row, index) {
                    return '<a href="javascript:;" class="searchit btn-class_id" data-toggle="tooltip" data-original-title="点击搜索 ' + row.class.name + '"> ' + row.class.name + '</a>';
                },
            },
            events: {//绑定事件的方法
                class_id: {
                    //格式为：方法名+空格+DOM元素
                    'click .btn-class_id': function (e, value, row, index) {
                        e.stopPropagation();
                        var container = $("#table").data("bootstrap.table").$container;
                        //这里我们手动将数据填充到表单然后提交
                        $("form.form-commonsearch [name='class_id']", container).val(value);
                        $("form.form-commonsearch", container).trigger('submit');
                    }
                },
            }
        }
    };
    return Controller;
});
